import { Records } from '@/api/plan/gantt/model/type';
export default () => {
    const wrapperRef = ref<HTMLDivElement>();
    const startRenderNum = ref(0);
    const endRenderNum = ref(0);
    const sliceData = (
        unVisibleH: number,
        wrapperH: number,
        wrapperT: number,
        cellHeight: number,
        data: Records[]
    ) => {
        if (!wrapperRef.value) return;
        const windowHeight = window.innerHeight;
        const parentBottom = windowHeight - wrapperT - wrapperH; // 父元素距离底部的距离
        let startPosition = 0;
        let endPosition = 0;
        const ClientRect = wrapperRef.value.getBoundingClientRect();
        const top = ClientRect.top;
        const bottom = ClientRect.bottom;

        if (top <= 0) {
            startPosition = Math.abs(top) + unVisibleH;
            endPosition = startPosition + wrapperH;
            if (bottom > unVisibleH && bottom <= windowHeight) {
                endPosition = startPosition + bottom;
            } else if (bottom <= unVisibleH) {
                startRenderNum.value = 0;
                endRenderNum.value = 0;
                return;
            }
        } else if (top > 0 && top <= unVisibleH) {
            // 不可视区域 - 滚动区域top - 父元素距离底部的距离 - 1行高度
            startPosition = unVisibleH - top - parentBottom - cellHeight;
            endPosition = startPosition + wrapperH + cellHeight;
        } else if (top > unVisibleH && top <= windowHeight) {
            startPosition = 0;
            endPosition = windowHeight - top;
        } else if (top > windowHeight) {
            startRenderNum.value = 0;
            endRenderNum.value = 0;
            return;
        }

        //没有高度，不需要渲染元素
        if (wrapperH === 0 || cellHeight === 0) {
            startRenderNum.value = 0;
            endRenderNum.value = 0;
            return;
        }
        const endNum = Math.ceil(endPosition / cellHeight);

        if (endNum > data.length) {
            // 一屏能渲染的dom个数
            const visibleDOM = wrapperH / (cellHeight + 4);

            const start = data.length - visibleDOM;
            endRenderNum.value = data.length;
            startRenderNum.value = start < 0 ? 0 : start;
        } else {
            const startNum = Math.ceil(startPosition / cellHeight);
            endRenderNum.value = endNum;
            startRenderNum.value = startNum < 0 ? 0 : startNum;
        }
    };
    return {
        startRenderNum,
        endRenderNum,
        sliceData,
        wrapperRef
    };
};
